import CheckFocusAccount from '../../modules/check_focus_account.js';
import CheckUserViewResult from '../../modules/count_view_result.js';
import RTLog from '../../modules/rt_log.js';
import Mocha from '../../modules/mocha.js';
const App = getApp();

Page({
    mpid: '',
    linked: '',
    data: {
        status: 0,
        resultImageUrl: '',
        taskId: '',
    },
    onLoad: function (options) {
        this.mpid = options.mpid || '';
        this.linked = options.linked || false;
        if (options.accountUrl) {
            this.setData({
                url: decodeURIComponent(options.accountUrl),
                resultImageUrl: options.resultUrl || '',
                taskId: options.taskId || ''
            });
        }
    },
    onShow() {
        if (this.data.status !== 0) {
            wx.navigateBack({ delta: 1 });
        }
        this.setData({ status: this.data.status + 1 });
    },
    webLoad() {
        if (this.linked === false) {
            this.MakeImageTaskResult();
            const unionid = getApp().mox.user.unionid;
            const appid = getApp().globalData.accountInfo.appid;
            this.timer = setInterval(() => {
                CheckFocusAccount.request('GET', `${unionid}?appid=${appid}`).then((res) => {
                    if (res[0] && res[0]?.status === 1) {
                        wx.reLaunch({
                            url: `/pages/result/result?mpid=${this.mpid}&url=${this.data.resultImageUrl}`
                        });
                        clearInterval(this.timer);
                        CheckUserViewResult.request('POST').catch((err) => {
                            // getApp().mox.stat(
                            //     `web page add_count_view_result fail:${
                            //         err.errMsg || err.msg || '未知错误'
                            //     }`
                            // );
                            getApp().mox.stat(`web page add_count_view_result fail`);
                            RTLog.error(
                                'web page add_count_view_result fail：',
                                err.errMsg || err.msg || '未知错误'
                            );
                        });
                        getApp().mox.stat(`webview_auto_jump`);
                    }
                });
            }, 400);
        }
    },
    MakeImageTaskResult() {
        return new Promise((resolve, reject) => {
            let answers = App.globalData.answers || ['1', '1', '1', '1'];
            if (answers[0] != -1) {
                answers.unshift(-1);
            }
            Mocha.createMakeImageTask(this.mpid, answers)
                .then((resTaskId) => {
                    this.urlTime = setInterval(() => {
                        Mocha.getMakeImageTaskResult(resTaskId)
                            .then((result) => {
                                if (!result.url) {
                                    getApp().mox.stat('web page get_result_image_url fail：no url');
                                    // RTLog.error('result page get_result_image_url fail：', 'no url');
                                } else {
                                    clearInterval(this.urlTime);
                                    getApp().mox.stat('web page get_result_image_url success');
                                    this.setData({ resultImageUrl: result.url });
                                }
                            })
                            .catch((err) => {
                                // getApp().mox.stat(
                                //     `web page get_result_image_url fail:${
                                //         err.errMsg || err.msg || '未知错误'
                                //     }`
                                // );
                                if ((err.errMsg || err.msg) == '查询出错') return;
                                getApp().mox.stat(`web page get_result_image_url fail`);
                                // RTLog.error(
                                //     'web page get_result_image_url fail：',
                                //     err.errMsg || err.msg || '未知错误'
                                // );
                                reject(err);
                            });
                    }, 500);
                    getApp().mox.stat('web page make_task success');
                })
                .catch((err) => {
                    // getApp().mox.stat(
                    //     `web page make_task fail:${err.errMsg || err.msg || '未知错误'}`
                    // );
                    getApp().mox.stat(`web page make_task fail`);
                    // RTLog.error(
                    //     'web page make_task fail：',
                    //     err.errMsg || err.msg || '未知错误'
                    // );
                    reject(err);
                });
        });
    },

    onHide() {
        clearInterval(this.timer);
        clearInterval(this.urlTime);
    },
    onUnload() {
        clearInterval(this.timer);
        clearInterval(this.urlTime);
    }
});
